package cn.luckdou.commons.utils;

import cn.luckdou.commons.pojo.Simage;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;

public class NewImageUtils {

    /**
     * 将 Simage的图片画在D上面
     * @param D
     * @param simage
     * @return
     */
    public static BufferedImage addImage(BufferedImage D,Simage simage){
        Graphics2D g2d = D.createGraphics();
        g2d.drawImage(simage.getImage(), simage.getX(), simage.getY(), simage.getImage().getWidth(), simage.getImage().getHeight(), null);
        g2d.dispose();// 释放图形上下文使用的系统资源
        return D;
    }

    public static BufferedImage addFont(BufferedImage D,String s,int y){
        Graphics2D g2d = D.createGraphics();
        Font font = new Font("黑体", Font.PLAIN, 25);
        g2d.setFont(font);              //设置字体
        g2d.drawString(s, (400-(s.length()*25))/2, y);  //画出水印
        g2d.dispose();// 释放图形上下文使用的系统资源
        return  D;
    }

    public static BufferedImage getBufferedImageByUrl(String url) throws Exception {
        BufferedImage buffImg = ImageIO.read(new URL(url));
        return buffImg;
    }

    public static BufferedImage getBufferedImageByFile(String file) throws IOException {
        BufferedImage buffImg = ImageIO.read(new File(file));
        return buffImg;
    }


    private static void generateWaterFile(BufferedImage buffImg, String savePath) {
        int temp = savePath.lastIndexOf(".") + 1;
        try {
            ImageIO.write(buffImg, savePath.substring(temp), new File(savePath));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    public  static String outBase64(BufferedImage D){
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        byte[] data = null;
        try {
            ImageIO.write(D, "jpeg", os);
            InputStream is = new ByteArrayInputStream(os.toByteArray());
            data = new byte[is.available()];
            is.read(data);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        return "data:image/jpeg;base64,"+encoder.encode(data);// 返回Base64编码过的字节数组字符串
    }

    public static void main(String[] args) throws IOException {
        String d = "/Users/mac/Desktop/d.jpg";
        String s = "http://wx.qlogo.cn/mmopen/QOvnWgL7qTIqENsMkXh0Vru2MsBWxcteSrmtrgerNbS96gJbJ5uZML5VnDKvvFbiaFawYoOeCotxcBYibIicVmSxVy0RZuT5gnt/132";
        String c = "http://img.havezone.com/img/1509335157209094.jpg";
        String saveFilePath = "/Users/mac/Desktop/out.jpg";

        try{
            BufferedImage D = getBufferedImageByFile(d);
            BufferedImage S = getBufferedImageByUrl(s);
            BufferedImage C = getBufferedImageByUrl(c);

            D = addImage(D, new Simage(S, 134, 120));
            D = addFont(D, "你好啊",285);
            D = addFont(D, "最适合你生存的国家",380);
            D = addImage(D, new Simage(C, 100, 410));

            System.out.println(outBase64(D));
//            generateWaterFile(D, saveFilePath);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}